package com.security.manage.user.controller;

import com.security.manage.user.dto.TaskResp;
import com.security.manage.user.model.*;
import com.security.manage.user.repository.VpnInfoRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class VpnInfoController {

    @Resource
    private VpnInfoRepository vpnInfoRepository;

    @PostMapping("/vpn")
    public ResData addVpn(@RequestBody List<VpnInfo> vpnInfos){

        vpnInfoRepository.saveAll(vpnInfos);
        return new ResData(1,"操作成功");
    }

    @DeleteMapping("/vpn/{id}")
    public ResData delVpn(@PathVariable("id") Long id){

        vpnInfoRepository.deleteById(id);
        return new ResData(1,"操作成功");
    }

    @GetMapping("/vpn/list")
    public ResData listVpn(@RequestParam(value = "page",defaultValue = "1") Integer page,
                            @RequestParam(value = "size",defaultValue = "10") Integer size){

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        PageRequest of = PageRequest.of(page, size, sort);
        Page<VpnInfo> pages = vpnInfoRepository.findAll(of);

        Map<String,Object> map = new HashMap<>();
        map.put("total",pages.getTotalElements());
        map.put("data",pages.get().collect(Collectors.toList()));

        return new ResData<>(1, "获取成功", map);
    }

    /**
     * search 服务调用 ，获取所有的vpn ip 列表返回list string 数组
     * @return
     */
    @GetMapping("/vpn/all")
    public List<String> listTask() {

        List<VpnInfo> pages = vpnInfoRepository.findAll();
        return pages.stream().map(VpnInfo::getVpnIp).collect(Collectors.toList());

    }

}
